//
//  OrderedFoodViewController.m
//  Karaoke
//
//  Created by 蒋 浩锋 on 12-8-13.
//  Copyright (c) 2012年 Jiang Haofeng. All rights reserved.
//

#import "OrderedFoodViewController.h"
#import "OrderFoodCell.h"
#import "InputPwdView.h"
#import "RoomInfoBarView.h"

@interface OrderedFoodViewController () <UITableViewDataSource, UITableViewDelegate, SocketDataHandleDelegate>
{
    __weak IBOutlet UITableView *tblView;
    __weak IBOutlet FXLabel     *lblAmount;
    __weak IBOutlet UILabel     *lblQuantity;
    __weak IBOutlet UIButton    *btnConfirm;
    __weak IBOutlet UIImageView *sdLogo;
    __weak IBOutlet UIImageView *syLogo;
    RoomInfoBarView             *roomInfobar;
}

@end

@implementation OrderedFoodViewController
@synthesize tempFoodObject;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    roomInfobar = [[RoomInfoBarView alloc] initWithXib];
    roomInfobar.frame = CGRectMake(10.0f, 19, roomInfobar.frame.size.width, roomInfobar.frame.size.height);
    [self.view addSubview:roomInfobar];
    [self calcAmount];
    [SocketDataHandle sharedInstance].delegate = self;
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    tblView = nil;
    [self setTempFoodObject:nil];
    lblAmount = nil;
    lblQuantity = nil;
    btnConfirm = nil;
    syLogo = nil;
    sdLogo = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (IBAction)DeleteAllFoodAction:(id)sender
{
    [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet removeAllObjects];
    [self calcAmount];
    [tblView reloadData];
}

- (IBAction)confirmOrderAction:(id)sender
{
    if (![[ServerNetworkCenter sharedInstance] getConnected])
    {
        UIAlertView *alert = [[UIAlertView alloc]   initWithTitle       :@"提示信息"
                                                    message             :@"当前网络未链接！请检查网络链接后再试。"
                                                    delegate            :nil
                                                    cancelButtonTitle   :@"确定"
                                                    otherButtonTitles   :nil, nil];
        [alert show];
        return;
    }

    if (![SocketDataHandle sharedInstance].tmpSelectFood.foodSet.count > 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]   initWithTitle       :@"提示信息"
                                                    message             :@"您还没有点选菜品！"
                                                    delegate            :nil
                                                    cancelButtonTitle   :@"确定"
                                                    otherButtonTitles   :nil, nil];
        [alert show];
        return;
    }

    ((UIButton *)sender).enabled = NO;
    InputPwdView *pwdAlert = [[InputPwdView alloc] init];
    pwdAlert.JKdelegate = self;
    [pwdAlert show];
}

- (IBAction)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)calcAmount
{
    // Do any additional setup after loading the view from its nib.
    int     sl = 0;
    float   je = 0;

    for (OKFolioObject *obj in [SocketDataHandle sharedInstance].tmpSelectFood.foodSet)
    {
        if (![obj.Flag isEqualToString:@"F"])
        {
            sl += obj.Quantity;
            je += [obj.Price floatValue] * obj.Quantity;
        }
    }

    lblAmount.text = [NSString stringWithFormat:@"%8.2f", je];
    lblQuantity.text = [NSString stringWithFormat:@"%d", sl];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [SocketDataHandle sharedInstance].tmpSelectFood.foodSet.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderFoodCell *cell = (OrderFoodCell *)[tableView dequeueReusableCellWithIdentifier:@"OrderFoodCell"];

    //    cell.orderFoodView = self;
    cell.btnDelete.tag = indexPath.row;

    OKFolioObject *obj = [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet objectAtIndex:indexPath.row];
    cell.lblFoodName.text = obj.FoodName;
    cell.lblFoodUntiPrice.text = obj.Price;
    cell.lblFoodNum.text = [NSString stringWithFormat:@"%d", obj.Quantity];
    cell.lblFoodTotalPrice.text = [NSString stringWithFormat:@"%8.2f", [obj.Price floatValue] * obj.Quantity];

    if ([obj.Flag isEqualToString:@"N"] && [obj.IfPackChild isEqualToString:@"1"])
    {
        cell.lblFoodState.text = @"套餐内项目";
        //        cell.imgBg.image = [UIImage imageNamed:@"bill1.png"];
        cell.btnDelete.hidden = YES;
    }
    else
    if ([obj.Flag isEqualToString:@"F"])
    {
        cell.lblFoodState.text = @" 赠送";
        //            cell.imgBg.image = [UIImage imageNamed:@"bill1.png"];
        cell.btnDelete.hidden = YES;
    }

    cell.btnDelete.tag = indexPath.row;
    [cell.btnDelete addTarget:self action:@selector(deleteSelect:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{}

- (void)socketDataHandleDidConfirmInputFood:(NSString *)errorMsg
{
    if (!errorMsg.length > 0)
    {
        [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet removeAllObjects];
        [self calcAmount];
        [self backAction:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]   initWithTitle       :@"提示信息"
                                                    message             :errorMsg
                                                    delegate            :self
                                                    cancelButtonTitle   :@"确定"
                                                    otherButtonTitles   :nil, nil];
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"click alertview btn %d", buttonIndex);

    if (buttonIndex == 1)
    {
        NSString *sPass = ((InputPwdView *)alertView).txtPwd.text;

        if (sPass && ![sPass isEqualToString:@""])
        {
            NSLog(@"spass %@", sPass);
            NSMutableArray *para = [[NSMutableArray alloc] initWithObjects:
                [DataCenter sharedInstance].roomNo
                , @"2"
                , @"1"
                , [DataCenter sharedInstance].clerkNo
                , sPass
                , lblAmount.text
                , [NSString stringWithFormat:@"%d", [SocketDataHandle sharedInstance].tmpSelectFood.foodSet.count]
                , nil];

            for (OKFolioObject *obj in [SocketDataHandle sharedInstance].tmpSelectFood.foodSet)
            {
                [para addObject:obj.FoodNo];
                [para addObject:obj.FoodName];
                [para addObject:obj.Price];
                [para addObject:obj.Flag];
                [para addObject:[NSString stringWithFormat:@"%d", obj.Quantity]];
                [para addObject:obj.CanDiscount];
                [para addObject:obj.Adds1Name];
                [para addObject:obj.Adds2Name];
                [para addObject:obj.Monad];
                [para addObject:obj.IfLowLimitPrice];
                [para addObject:[NSString stringWithFormat:@"%@%@", obj.PrinterNo, [DataCenter sharedInstance].printId]];
                [para addObject:obj.OverTimes];
                [para addObject:obj.IfPack];
                [para addObject:obj.IfPackChild];

                [para addObject:obj.AutoFreed];
                [para addObject:obj.AutoFreeCount];
                [para addObject:obj.GroupID];
                [para addObject:obj.ParentFoodNo];

                [para addObject:obj.CanReward];
                [para addObject:obj.LimitDisRate];
                [para addObject:obj.Note];

                [para addObject:obj.DeduckClerkName];
                [para addObject:obj.FlowerGirlsNo];
                [para addObject:obj.UseVipSpecialPrice];// 会员特价
                [para addObject:obj.VipPrice];
                [para addObject:obj.retain1];
                [para addObject:obj.retain2];
                [para addObject:obj.retain3];
                [para addObject:obj.retain4];
                [para addObject:obj.retain5];
            }

            [[ServerNetworkCenter sharedInstance] sendMsg:rtConfirmInputFood:para];
        }
        else
        {
            btnConfirm.enabled = YES;
            [alertView show];
        }
    }
    else
    {
        btnConfirm.enabled = YES;
    }
}

- (void)deleteSelect:(id)sender
{
    OKFolioObject   *foodObj = [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet objectAtIndex:((UIButton *)sender).tag];
    NSMutableArray  *tempArray = [NSMutableArray arrayWithArray:[SocketDataHandle sharedInstance].tmpSelectFood.foodSet];

    if ([foodObj.IfPack isEqualToString:@"1"])
    {
        for (OKFolioObject *obj in tempArray)
        {
            if ([obj.ParentFoodNo isEqualToString:foodObj.FoodNo])
            {
                [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet removeObject:obj];
            }
        }
    }
    else
    {
        for (OKFolioObject *obj in tempArray)
        {
            if (![obj.GroupID isEqualToString:@""] && [obj.GroupID isEqualToString:foodObj.GroupID])
            {
                [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet removeObject:obj];
            }
        }
    }

    [[SocketDataHandle sharedInstance].tmpSelectFood.foodSet removeObject:foodObj];
    [self calcAmount];
    [tblView reloadData];
}

@end